using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DM.DAO;

namespace DM.Web.Admin.QuanLyChucNangKhac
{
    public partial class QuanLySliderAnh : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack) BindingDataList();
        }

        private void BindingDataList()
        {
            DataTable table = new DataTable();
            table.Columns.Add("urlImage");

            DirectoryInfo dirSlide = new DirectoryInfo(MapPath("../../Photos/Slide"));
            if (dirSlide.Exists)
            {
                foreach (FileInfo file in dirSlide.GetFiles())
                {
                    DataRow row = table.NewRow();
                    row["urlImage"] = file.Name;
                    table.Rows.Add(row);
                }
                GridViewSlideAnh.DataSource = table;
                GridViewSlideAnh.DataBind();
            }
            else
            {
                Common.Show("Thư mục không tồn tại, xem lại code xem nào!", Request.UrlReferrer.ToString());
            }
        }

        protected void GridViewSlideAnh_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewSlideAnh.PageIndex = e.NewPageIndex;
            BindingDataList();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Button button = (Button) sender;
            GridViewRow current = (GridViewRow) button.Parent.Parent;
            string linkanh = GridViewSlideAnh.DataKeys[current.RowIndex].Value.ToString();

            FileInfo file = new FileInfo(MapPath("~/Photos/Slide/"+linkanh));
            if (file.Exists)
            {
                file.Delete();
            }

            Common.Show("Đã xóa ảnh!", Request.UrlReferrer.ToString());
        }

        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            if (fileUpLoadImage.HasFile)
            {
                string ext = Path.GetExtension(fileUpLoadImage.FileName).ToUpper();
                if (ext == ".JPG" || ext == ".GIF" || ext == ".JPEG" ||
                    ext == ".PNG")
                {
                    Common.UploadAnhSlide(fileUpLoadImage, Request.PhysicalApplicationPath + "Photos/Slide/");
                    Common.Show("Upload ảnh thành công", Request.UrlReferrer.ToString());
                }
                Common.Show("Bạn chọn sai định dạng file ảnh, vui lòng chọn chính xác!",Request.UrlReferrer.ToString());
                
            }
            else
            {
                Common.Show("Bạn chưa chọn ảnh, upload không thành công!", Request.UrlReferrer.ToString());
            }
            
        }
        
    }
}
